#include "roundworld.h"

Roundworld::Roundworld( Textverbosemessage* textverbosemessage )
 : WorldB( textverbosemessage )
{
  dummyEntity = new Entity(WALL, BeColor(0.3f, 0.2f, 0.1f, 1.0f));
}

void Roundworld::process_prephysics(unsigned long long frameCounter)
{
  WorldB::process_prephysics(frameCounter);

		// adjust gravity vectors of all entities' rigid bodies
		unsigned int j, b;
		btRigidBody* bo;
		
		for( j=0; j < entities.size(); j++)
		{	
			if ( entities[j]->type == FOOD )
			{
				Food* f = static_cast<Bodypart*>( entities[j] )->getOwner_food();
				for( b=0; b < f->body.bodyparts.size(); b++)
				{
					bo = f->body.bodyparts[b]->body;
					bo->setGravity( -(bo->getCenterOfMassPosition().normalized()*10) );
				}
			}
		}
		for( j=0; j < critters.size(); j++)
		{
			CritterB* bod = critters[j];
			for( b=0; b < bod->body.bodyparts.size(); b++)
			{
				bo = bod->body.bodyparts[b]->body;
				bo->setGravity( -(bo->getCenterOfMassPosition().normalized()*10) );
			}
		}
}

void Roundworld::makeFloor()
{
	groundTransform.setIdentity();
	groundTransform.setOrigin( btVector3(0,0,0) );

	groundShape = new btSphereShape(btScalar( *worldsizeX ));
	
	fixedGround = new btCollisionObject();
	fixedGround->setUserPointer(dummyEntity);
	fixedGround->setCollisionShape(groundShape);
	fixedGround->setWorldTransform(groundTransform);
	m_dynamicsWorld->addCollisionObject(fixedGround);
}


void Roundworld::drawWithGrid()
{
	glClear(GL_STENCIL_BUFFER_BIT);
	glEnable(GL_CULL_FACE);
	glCullFace(GL_BACK);

	glPushMatrix(); 
		glTranslatef(0,0,0);
		glColor4f( 0.3f, 0.2f, 0.1f, 1.0f );
		drawSphere(
			*worldsizeX,
			(12.56637* *worldsizeX / 6),
			(12.56637* *worldsizeX / 3)
		);
	glPopMatrix(); 
	for( unsigned int i=0; i < critters.size(); i++)
		critters[i]->draw();

	for( unsigned int i=0; i < entities.size(); i++)
		entities[i]->draw();

	glDisable(GL_STENCIL_TEST);
	glDisable(GL_CULL_FACE);
}

void Roundworld::childPositionOffset(btVector3* v)
{
	*v+= (v->normalized()*insertHight);
}

btVector3 Roundworld::findPosition()
{
	return btVector3(	((float)randgen->Instance()->get( 0, 200 )-100.0f) / 100,
				((float)randgen->Instance()->get( 0, 200 )-100.0f) / 100,
				((float)randgen->Instance()->get( 0, 200 )-100.0f) / 100
	).normalized() * ( *worldsizeX + insertHight);
}

void Roundworld::drawfloor()
{
	glPushMatrix(); 
		glTranslatef(0,0,0);
		glColor4f( 0.3f, 0.2f, 0.1f, 1.0f );
		drawSphere(
			*worldsizeX,
			(12.56637* *worldsizeX / 12),
			(12.56637* *worldsizeX / 6)
		);
	glPopMatrix(); 
}

void Roundworld::drawSphere(btScalar radius, int lats, int longs) 
{
	int i, j;
	for(i = 0; i <= lats; i++) {
		btScalar lat0 = SIMD_PI * (-btScalar(0.5) + (btScalar) (i - 1) / lats);
		btScalar z0  = radius*sin(lat0);
		btScalar zr0 =  radius*cos(lat0);

		btScalar lat1 = SIMD_PI * (-btScalar(0.5) + (btScalar) i / lats);
		btScalar z1 = radius*sin(lat1);
		btScalar zr1 = radius*cos(lat1);

		glBegin(GL_QUAD_STRIP);
		for(j = 0; j <= longs; j++) {
			btScalar lng = 2 * SIMD_PI * (btScalar) (j - 1) / longs;
			btScalar x = cos(lng);
			btScalar y = sin(lng);
			glNormal3f(x * zr1, y * zr1, z1);
			glVertex3f(x * zr1, y * zr1, z1);
			glNormal3f(x * zr0, y * zr0, z0);
			glVertex3f(x * zr0, y * zr0, z0);
		}
		glEnd();
	}
}

Roundworld::~Roundworld()
{
	m_dynamicsWorld->removeCollisionObject(fixedGround);
	delete groundShape;
	delete fixedGround;
  delete dummyEntity;
}
